package com.starblink.android.basic.widget.recycler

import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.starblink.android.basic.widget.recycler.BottomLineItemDecor.Companion.noBottomLine
import com.starblink.android.basic.widget.recycler.BottomLineItemDecor.Companion.noMoreData
import com.starblink.android.basic.widget.recycler.BottomLineItemDecor.Companion.refresh

/**
 * SmartRefreshLayout和RecyclerView的bottom line接口处理类,bottom line将显示在recyclerview 底部
 *
 *  Create by yuanl at 2023/10/27
 **/
class RefresherAndListCombiner(val refresher: SmartRefreshLayout, val list: RecyclerView) {

    init {
        refresher.setEnableAutoLoadMore(true)
        refresher.noBottomLine()
    }


    fun setNoMoreData(noMoreData: Boolean) {
        refresher.setNoMoreData(noMoreData)
        if (!noMoreData) {
            list.refresh()
        }
    }

    /**
     * 复写刷新时间
     */
    fun onRefresh(onRefreshed: () -> Unit) {
        refresher.setOnRefreshListener {
            onRefreshed.invoke()
        }
    }

    /**
     * 复写加载更多事件
     */
    fun onLoadMore(onLoadMore: () -> Unit) {
        refresher.setOnLoadMoreListener {
            onLoadMore.invoke()
        }
    }

    fun finishRefresh() {
        refresher.finishRefresh()
        list.refresh()
    }

    fun finishLoadMore() {
        refresher.finishLoadMore()
    }

    fun finishLoadMoreNoData(textColor: Int ? = null, lineColor: Int ?= null) {
        refresher.finishLoadMoreWithNoMoreData()
        list.noMoreData(textColor, lineColor)
    }

}